package top.ffhome.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import top.ffhome.common.annotation.DataScope;
import top.ffhome.common.core.mybatisplus.service.impl.BaseServiceImpl;
import top.ffhome.system.domain.SysLogininfor;
import top.ffhome.system.mapper.SysLogininforMapper;
import top.ffhome.system.service.ISysLogininforService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl extends BaseServiceImpl<SysLogininforMapper, SysLogininfor>
        implements ISysLogininforService {
    @Override
    protected QueryWrapper<SysLogininfor> generateWrapper(SysLogininfor query) {
        Map<String, Object> params = query.getParams();
        return new QueryWrapper<SysLogininfor>()
                .select("l.*")
                .like(StrUtil.isNotBlank(query.getIpaddr()), "l.ipaddr", query.getIpaddr())
                .eq(StrUtil.isNotBlank(query.getStatus()), "l.status", query.getStatus())
                .like(StrUtil.isNotBlank(query.getUserName()), "l.user_name", query.getUserName())
                .apply(ObjectUtil.isNotEmpty(params.get("beginTime")),
                        "date_format(l.login_time,'%y%m%d') >= date_format({0},'%y%m%d')",
                        params.get("beginTime"))
                .apply(ObjectUtil.isNotEmpty(params.get("endTime")),
                        "date_format(l.login_time,'%y%m%d') <= date_format({0},'%y%m%d')",
                        params.get("endTime"))
                .orderByDesc("l.info_id");
    }

    @Override
    @DataScope(deptAlias = "u.dept_id", userAlias = "u.user_id")
    public List<SysLogininfor> selectList(SysLogininfor query) {
        return super.selectList(query);
    }

    @Override
    protected void beforeInsert(SysLogininfor entity) {
        entity.setLoginTime(new Date());
        super.beforeInsert(entity);
    }

    @Override
    public void cleanLogininfor() {
        mapper.cleanLogininfor();
    }
}
